package com.tarena.lbs.basic.web.dao.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.basic.web.dao.mapper.AdminMapper;
import com.tarena.lbs.pojo.basic.po.AdminPO;
import com.tarena.lbs.pojo.basic.query.AdminQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 注入mapper持久层 但是mapper继承的BaseMapper只能操作单表 单次增删查改
 * mybatisPlus有上层封装可以在仓储层实现ServiceImpl<A,B>
 */
@Repository
@Slf4j
public class AdminRepository extends ServiceImpl<AdminMapper,AdminPO> {
    @Autowired
    private AdminMapper adminMapper;

    public PageInfo<AdminPO> accountPage(AdminQuery adminQuery) {
        QueryWrapper<AdminPO> queryWrapper = assemBelQuery(adminQuery);
        Integer pageNo = adminQuery.getPageNo();
        Integer pageSize = adminQuery.getPageSize();
        //1.使用pageHelper开启一个分页执行
        PageHelper.startPage(pageNo, pageSize);
        //2.直接使用持久层 查询与业务条件queryWrapper有关的账号列表
        List<AdminPO> adminPOS = adminMapper.selectList(queryWrapper);
        //3.将列表查询结果 包装到一个PageInfo对象里 触发封装分页的条件
        PageInfo<AdminPO> pageInfo = new PageInfo<>(adminPOS);
        log.info("当前页:{}",pageInfo.getPageNum());//pageNo
        log.info("每页条:{}",pageInfo.getPageSize());//pageSize
        log.info("总页数:{}",pageInfo.getTotal());//total
        log.info("分页数据:{}",pageInfo.getList());//object
        return pageInfo;
    }

    private static QueryWrapper<AdminPO> assemBelQuery(AdminQuery adminQuery) {
        QueryWrapper<AdminPO> queryWrapper = new QueryWrapper<>();
        if(adminQuery.getAccountPhone()!=null){
            queryWrapper.like("account_phone", adminQuery.getAccountPhone());
        }
        if(adminQuery.getNickname()!=null){
            queryWrapper.like("nickname", adminQuery.getNickname());
        }
        return queryWrapper;
    }
}
